/*
 * Creative Commons Deed
 * Attribution-NonCommercial-ShareAlike 1.0 Finland
 * 
 * You are free:
 * 
 * * to copy, distribute, display, and perform the work
 * * to make derivative works
 * Under the following conditions:
 * 
 * Attribution. You must attribute the work in the manner specified by the author or licensor.
 * 
 * Noncommercial. You may not use this work for commercial purposes.
 * 
 * Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only 
 * under a license identical to this one.
 * 
 * * For any reuse or distribution, you must make clear to others the license terms of this work.
 * * Any of these conditions can be waived if you get permission from the copyright holder.
 * Your fair use and other rights are in no way affected by the above.
 * This is a human-readable summary of the Legal Code (the full license). 
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using PocketLibrary;

namespace PocketApps
{
    public partial class BlogiArtikkelit : Form
    {
        bool bGettingPosts = false;
        Thread workerThread;
        public PocketLibrary.BlogPost SelectedPost
        {
            get
            {
                if (listView1.SelectedIndices.Count > 0)
                {
                    PocketLibrary.BlogPost _ret = new PocketLibrary.BlogPost();
                    if (selectedBlog.Type == BlogType.Blogger)
                    {
                        _ret.postBody = listView1.Items[listView1.SelectedIndices[0]].SubItems[2].Text;
                        _ret.ServiceEdit = listView1.Items[listView1.SelectedIndices[0]].SubItems[3].Text;
                    }
                    else
                        _ret.postId = listView1.Items[listView1.SelectedIndices[0]].SubItems[1].Text;

                    _ret.postTitle = listView1.Items[listView1.SelectedIndices[0]].Text;

                    return _ret;
                }

                else return null;
            }
        }

        public PocketLibrary.IBlog selectedBlog;

        ImageList postImages;
        int smallsize = DpiHelper.Scale(16);

        public BlogiArtikkelit()
        {
            InitializeComponent();
        }

        void Localize()
        {
            try
            {
                this.Text = PocketAppsLocalization.GetString("frmarticles");
                menuItem1.Text = PocketAppsLocalization.GetString("menucancel");
            }
            catch { }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void BlogiArtikkelit_Closing(object sender, CancelEventArgs e)
        {
            if (bGettingPosts == true)
                workerThread.Abort();

        }

        private void BlogiArtikkelit_Load(object sender, EventArgs e)
        {
            Localize();
            //selectedBlog.OnRequestProgress += new RequestProgressHandler(selectedBlog_OnRequestProgress);
            postImages = new ImageList();
            postImages.ImageSize = new Size(smallsize, smallsize);
            foreach (Image img in imageList1.Images)
            {
                postImages.Images.Add(img);
            }
            listView1.SmallImageList = postImages;

            //progressBar1.Visible = true;
            workerThread = new Thread(ThreadedPostList);
            workerThread.IsBackground = true;
            workerThread.Start();
        }

        void selectedBlog_OnRequestProgress(PocketLibrary.RequestProgressArgs args)
        {
            if (progressBar1 != null)
            {
                progressBar1.Invoke(new EventHandler(delegate
                {
                    progressBar1.Value = args.Progress;
                    Debug.WriteLine("method " + args.ApiMethod + " at " + args.Progress);
                }));
            }
        }

        void ThreadedPostList()
        {
            try
            {
                bGettingPosts = true;
                progressBar1.Invoke(new EventHandler(delegate
                {
                    progressBar1.Value = 0;
                }));
                PocketLibrary.BlogPost[] posts = selectedBlog.getRecentPosts(15);
                for (int i = 0; i < posts.Length; i++)
                {
                    /*
                    listBox1.Invoke(new EventHandler(delegate
                    {
                        listBox1.Items.Add(posts[i]);
                    }));
                     * */

                    ListViewItem _item = new ListViewItem();
                    _item.Text = posts[i].postTitle;
                    _item.SubItems.Add(posts[i].postId);

                    if (posts[i].postBody != null)
                        _item.SubItems.Add(posts[i].postBody);

                    if (posts[i].ServiceEdit != null)
                        _item.SubItems.Add(posts[i].ServiceEdit);

                    if (posts[i].publish == "true" || posts[i].publish == "1")
                        _item.ImageIndex = 0;
                    else if (posts[i].publish == "false" || posts[i].publish == "0")
                        _item.ImageIndex = 1;
                    else
                        _item.ImageIndex = 2;

                    listView1.Invoke(new EventHandler(delegate
                    {
                        listView1.Items.Add(_item);
                    }));

                }
                progressBar1.Invoke(new EventHandler(delegate
                {
                    progressBar1.Visible = false;
                }));
                listView1.Invoke(new EventHandler(delegate
                {
                    listView1.Visible = true;
                }));
                bGettingPosts = false;
            }
            catch (ThreadAbortException)
            { }

        }
    }
}